package com.lbloca.locate.weight;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lbloca.locate.R;

/**
 * Created by Administrator on 2018/4/16.
 */

public class ConversationDialog implements View.OnClickListener{
    private TextView closebutton;
    private TextView tv;
    private TextView openbutton;
    private Dialog dialog;
    private Activity context;
    private View view;

    public ConversationDialog(Activity context) {
        this.context = context;
    }

    public ConversationDialog builder() {
        view = LayoutInflater.from(context).inflate(R.layout.dialog_conversation, null);
        tv = view.findViewById(R.id.commondialog_tv);
        closebutton = view.findViewById(R.id.commondialog_close);
        closebutton.setOnClickListener(this);
        openbutton = view.findViewById(R.id.commondialog_openvip);
        openbutton.setOnClickListener(this);
        dialog = new Dialog(context, R.style.AlertDialogStyle);
        dialog.setContentView(view);
//        setDialogBack();
        return this;
    }

    public void setGravity(int gravity) {
        if (tv != null)
            tv.setGravity(gravity);
    }

    public boolean isShowing() {
        if (dialog != null)
            return dialog.isShowing();
        return false;
    }

    public void show() {
        if (dialog != null)
            dialog.show();
    }

    public void dismiss() {
        if (dialog != null)
            dialog.dismiss();
    }

    public void settitlemessage(String titlemessage) {
        if (tv != null)
            tv.setText(titlemessage);
    }

    /**
     * 设置右边按钮文字颜色
     *
     * @param color
     * @return
     */
    public ConversationDialog setRightBtnTextColor(int color) {
        openbutton.setTextColor(color);
        return this;
    }

    /**
     * 设置右边按钮内容
     *
     * @param btnText
     * @return
     */
    public ConversationDialog setRightBtnText(String btnText) {
        openbutton.setText(btnText);
        return this;
    }

    /**
     * 设置左边按钮内容
     *
     * @param btnText
     * @return
     */
    public ConversationDialog setLeftBtnText(String btnText) {
        closebutton.setText(btnText);
        return this;
    }


    public ConversationDialog setCancelable(boolean cancel) {
        dialog.setCancelable(cancel);
        return this;
    }

    public ConversationDialog setDialogBack() {
        dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    dismiss();
                }
                return false;
            }
        });
        return this;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.commondialog_close:
                if (mOnClickListener != null) {
                    mOnClickListener.clickLeftBtn();
                }
                break;
            case R.id.commondialog_openvip:
                if (mOnClickListener != null) {
                    mOnClickListener.clickRightBtn();
                }

        }
    }


    public void setBtnText(int id) {
        openbutton.setText(id);
    }


    private OnClickListener mOnClickListener;

    public void setOnClickListener(OnClickListener onClickListener) {
        this.mOnClickListener = onClickListener;
    }


    public interface OnClickListener {
        void clickRightBtn();

        void clickLeftBtn();
    }
}
